
var _path = '/zpmng/';
//url:form add and edit jsp
var _formAddEditUrl="/admin123/coding/owWorkPlan/add_edit.jsp";
//url:form add action
var _formAddAction="/admin123/coding/owWorkPlan/owWorkPlanAjax_addData.action";
//url:form edit action
var _formEditAction="/admin123/coding/owWorkPlan/owWorkPlanAjax_editData.action";
//url:form remove action
var _formRemoveAction="/admin123/coding/owWorkPlan/owWorkPlanAjax_removeData.action";
//url:report
var _formReportAction="/admin123/coding/owWorkPlan/add_edit.jsp";





var _keyRandom='ow_work_plan';
//上个页面通过右击列表 传过来的 id和row; ps:右击模板x列，看模板列列表，自动加载列数，要传模板id找表名
var _prePageListRow = '${paramValues.row[0]}';
_prePageListRow = decodeURI(_prePageListRow);
var _prePageListId = '${paramValues._id[0]}';

//template table foreign key
var _foreignKey = 'a2user_id';
var _primaryKey = 'id';

//add dialog width and height
var _width4AddDialog = '0';
var _height4AddDialog = '0';
if(_width4AddDialog == '' || _width4AddDialog == '0')_width4AddDialog=800;
if(_height4AddDialog == '' || _height4AddDialog == '0')_height4AddDialog=400;
//dialog style, 取代上面的方法
var _isdialog4AddDialog = '1';
var _style4AddDialog = 'width:800px;height:400px;';
if(_style4AddDialog == '')_style4AddDialog='width:800px;height:400px';


/*
	hidden fileds when option has changed
	选择optionId时，filedList是要隐藏的域
	结构如下 ：
	var hiddenFileds = [
		{"optionId":"11","filedList":[{"text":"foreignkey"},{"text":"tablenamesql"}]},
		{"optionId":"12","filedList":[{"text":"tablenamehistorysql"},{"text":"sqlsum"}]}
	];
*/
var hiddenFileds = []


$(function(){
	$('#cc').calendar({
		validator: function(date){
			var now = new Date();
			var d1 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
			var d2 = new Date(now.getFullYear(), now.getMonth(), now.getDate());
			return d1<=date && date<=d2;
		}
	});
});

/**
	页面datagrid控件显示列元素：datagrid.columns
*/
var datagrid_columns = [[{"field":"id","hidden":true,"width":100,"title":"ID"},
	{"field":"companyId","hidden":"true","width":100,"title":"company_id"},
	/*
	//move to frozen columns
	{"field":"a2userId","width":100,"title":"出勤人员",
		formatter:function(value,row){
			return row.a2userName;
		},
		editor:{
			type:'combobox',
			options:{
				valueField:'keyName',
				textField:'valueName',
				editable:false,
				data:_selectOptions4a2userId,
				required:true
			}
		}
	},*/
	{"field":"owAreaId","width":100,"title":"出勤区域",
		formatter:function(value,row){
			return row.owAreaName;
		},
		editor:{
			type:'combobox',
			options:{
				valueField:'keyName',
				textField:'valueName',
				editable:false,
				data:_selectOptions4owAreaId,
				required:true
			}
		}
	},
	{"field":"startTime","width":120,"title":"开始时间"
		,editor:{
			type:'datetimebox',
			options:{
				sharedCalendar:'#cc',
				editable:false
			}
		}
	},
	{"field":"endTime","width":120,"title":"结束时间"
		,editor:{
			type:'datetimebox',
			options:{
				sharedCalendar:'#cc',
				editable:false
			}
		}
	},
	{"field":"owWorkType","title":"工作类型","width":80,
		formatter:function(value,row){
			return row.owWorkType_Str;
		},
		editor:{
			type:'combobox',
			options:{
				valueField:'keyName',
				textField:'valueName',
				editable:false,
				data:_owWorkType4addDialog,
				required:true
			}
		}
	},
	{"field":"owOffpostEventType","title":"离岗原因","width":80,
		formatter:function(value,row){
			return row.owOffpostEventType_Str;
		},
		editor:{
			type:'combobox',
			options:{
				valueField:'keyName',
				textField:'valueName',
				editable:false,
				data:_owOffpostEventType4addDialog
			}
		}
	},
	{"field":"content","width":200,"title":"工作内容","editor":"textbox"},
	{"field":"outworkerDate","hidden":"true","width":100,"title":"outworker_date"},
	{"field":"deleteflag","hidden":"true","width":100,"title":"deleteflag"},
	{"field":"createdate","hidden":"true","width":100,"title":"createdate"},
	{"field":"createman","hidden":"true","width":100,"title":"createman"},
	{"field":"modifydate","hidden":"true","width":100,"title":"modifydate"},
	{"field":"modifyman","hidden":"true","width":100,"title":"modifyman"},
	{"field":"createmanId","hidden":"true","width":100,"title":"createman_id"},
	{"field":"modifymanId","hidden":"true","width":100,"title":"modifyman_id"}
	,{"formatter":
		function(value,rowData,rowIndex){if(typeof(rowData.id) == 'undefined'){return '';};
			var btn = '';
			btn += '<a class=\"button\" style=\"margin-left:1px;\" onclick=\"editRow('+rowIndex+')\" href=\"javascript:void(0)\">详情<\/a>';
			btn += '<a class=\"button\"  style=\"margin-left:1px;\" onclick=\"plan1('+rowIndex+')\" href=\"javascript:void(0)\">违规登记<\/a>';
			btn += '<a class=\"button\"  style=\"margin-left:1px;\" onclick=\"plan2('+rowIndex+')\" href=\"javascript:void(0)\">执勤登记<\/a>';
			btn += '<a class=\"button\"  style=\"margin-left:1px;\" onclick=\"plan3('+rowIndex+')\" href=\"javascript:void(0)\">任务计划<\/a>';
			//btn += '<a class=\"button\"  style=\"margin-left:1px;\" onclick=\"owOffpostRecord('+rowIndex+')\" href=\"javascript:void(0)\">离岗报备<\/a>';
			btn += '<a class=\"button\"  style=\"margin-left:1px;\" onclick=\"removeRow('+rowIndex+')\" href=\"javascript:void(0)\">删除<\/a>';
			return btn;},"field":"opt","width":270,"title":"操作","align":"center"}

]]


//违规登记
function plan1(rowIndex){
	var row = $('#'+datagrid.datagridId).datagrid('getData').rows[rowIndex];
	var a2userId = row.a2userId;
	var outworkerDate = row.outworkerDate;
	var url = "/admin123/coding/owIllegalRegister/my.jsp";
	url = url+'?_id='+a2userId;
	url = url+'&_outworkerDate='+outworkerDate;
	window.parent.Open("违规登记"+a2userId,url);
}
//执勤登记
function plan2(rowIndex){
	var row = $('#'+datagrid.datagridId).datagrid('getData').rows[rowIndex];
	var a2userId = row.a2userId;
	var outworkerDate = row.outworkerDate;
	var url = "/admin123/coding/owRegisterData/my.jsp";
	url = url+'?_id='+a2userId;
	url = url+'&_outworkerDate='+outworkerDate;
	window.parent.Open("执勤登记"+a2userId,url);
}
//任务列表
function plan3(rowIndex){
	var row = $('#'+datagrid.datagridId).datagrid('getData').rows[rowIndex];
	var a2userId = row.a2userId;
	var outworkerDate = row.outworkerDate;
	var url = "/admin123/coding/owTask/my.jsp";
	url = url+'?_id='+a2userId;
	url = url+'&_outworkerDate='+outworkerDate;
	window.parent.Open("任务列表"+a2userId,url);
}

/*function owOffpostRecord(){
	alert("开发中...");
	//rightMenuItemClick('/admin123/coding/owOffpostRecord/my.jsp','0','300','400','离岗报备');
}*/

//===================== Row Editor Start ===============
var editIndex = undefined;
function endEditing(){
	var dg = $('#'+datagrid.datagridId);
	if (editIndex == undefined){return true}
	if (dg.datagrid('validateRow', editIndex)){
		//出勤区域
		var ed = dg.datagrid('getEditor', {index:editIndex,field:'owAreaId'});
		if(ed == undefined){editIndex = undefined;}
		var owAreaName = $(ed.target).combobox('getText');
		dg.datagrid('getRows')[editIndex]['owAreaName'] = owAreaName;
		//出勤人员
		var ed = dg.datagrid('getEditor', {index:editIndex,field:'a2userId'});
		var a2userName = $(ed.target).combobox('getText');
		dg.datagrid('getRows')[editIndex]['a2userName'] = a2userName;
		//工作类型
		var ed = dg.datagrid('getEditor', {index:editIndex,field:'owWorkType'});
		var owWorkType_Str = $(ed.target).combobox('getText');
		dg.datagrid('getRows')[editIndex]['owWorkType_Str'] = owWorkType_Str;
		//工作类型
		var ed = dg.datagrid('getEditor', {index:editIndex,field:'owOffpostEventType'});
		var owOffpostEventType_Str = $(ed.target).combobox('getText');
		dg.datagrid('getRows')[editIndex]['owOffpostEventType_Str'] = owOffpostEventType_Str;
		//
		dg.datagrid('endEdit', editIndex);
		//submit
		if(getChanges()>0){
			submitRow();
			accept();
		}
		//
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}

function submitRow(){
	var dg = $('#'+datagrid.datagridId);
	var row = dg.datagrid('getRows')[editIndex];
	var _data = {};
	var _url = _formEditAction;
	var _isAdd = false;
	_data.id = row.id;
	if(row.id == undefined){
		_data.id = '';
		_url = _formAddAction;
		_isAdd = true;
	}
	_data.a2userId = row.a2userId;
	_data.owAreaId = row.owAreaId;
	_data.startTime = row.startTime;
	_data.endTime = row.endTime;
	_data.owWorkType = row.owWorkType;
	_data.owOffpostEventType = row.owOffpostEventType;
	_data.content = row.content;

	$.ajax({
		type:"POST",
		url:_path+_url,
		dataType:"json",
		data:_data,
		success:function(msg){
			if(JSON.stringify(msg).indexOf('error:')==0){
				window.parent.error(JSON.stringify(msg));
			}else{
				if(msg[0].responseCode==0){
					alert(msg[0].responseContent);
				}else{
					if(_isAdd){
						editIndex = undefined;
						datagrid_fresh();
					}
				}
				return;
			}
		},
		error:function(er){
			window.parent.error(er);
		}
	});
}

function editRow(index){
	var dg = $('#'+datagrid.datagridId);
	//check
	var ed = dg.datagrid('getEditor', {index:editIndex,field:'owAreaId'});
	if(ed == undefined){editIndex = undefined;}

	if (editIndex != index){
		if (endEditing()){
			dg.datagrid('selectRow', index)
				.datagrid('beginEdit', index);
			editIndex = index;
		} else {
			dg.datagrid('selectRow', editIndex);
		}
	}
}

function append(){
	var dg = $('#'+datagrid.datagridId);
	if (endEditing()){
		dg.datagrid('appendRow',{status:'P'});
		editIndex = $('#dg').datagrid('getRows').length-1;
		dg.datagrid('selectRow', editIndex)
			.datagrid('beginEdit', editIndex);
	}
}
function removeit(){
	var dg = $('#'+datagrid.datagridId);
	if (editIndex == undefined){return}
	dg.datagrid('cancelEdit', editIndex)
		.datagrid('deleteRow', editIndex);
	editIndex = undefined;
}
function accept(){
	var dg = $('#'+datagrid.datagridId);
	if (endEditing()){
		dg.datagrid('acceptChanges');
	}
}
function reject(){
	var dg = $('#'+datagrid.datagridId);
	dg.datagrid('rejectChanges');
	editIndex = undefined;
}
function getChanges(){
	var dg = $('#'+datagrid.datagridId);
	var rows = dg.datagrid('getChanges');
	return rows.length;
}
//===================== Row Editor End ===============


/**
页面datagrid控件显示列元素：datagrid.frozenColumns
*/
var datagrid_frozenColumns = []
var jarrTmp = [{"field":"a2userName","width":100,"title":"出勤人员"}];
var jo = {
"formatter":function(value,rowData,rowIndex){
if(typeof(rowData.id) == 'undefined'){return '';};
var btn = '';
btn += '<a rowIndex=\"'+rowIndex+'\" class=\"button\" style=\"height:16px;\"  href=\"javascript:void(0)\">菜单<\/a>';
    return btn;
    },
    "field":"menuItem",
    "width":70,
    "title":"菜单列表",
    "align":"center",
    "hidden":true};
var jarr = [];
jarr.push(jo);
$.each(jarrTmp,function(index,value){
jarr.push(jarrTmp[index]);
})
datagrid_frozenColumns.push(jarr);


/**
	页面datagrid控件显示列元素：datagrid.toolbarBtns
*/
var datagrid_toolbarBtns = [];
datagrid_toolbarBtns[datagrid_toolbarBtns.length] = {
 id:'toolbarAddBtnId',text:'添加',
 iconCls:'icon-add',
 handler:function(){toolbar_add();}
}
/*datagrid_toolbarBtns[datagrid_toolbarBtns.length] = {
	id:'toolbarAddBtnId',text:'添加一行',
	iconCls:'icon-add',
	handler:function(){append();}
}*/
/*datagrid_toolbarBtns[datagrid_toolbarBtns.length] = {
	id:'toolbarAddBtnId',text:'接收',
	iconCls:'icon-add',
	handler:function(){endEditing();}
}*/
/*datagrid_toolbarBtns[datagrid_toolbarBtns.length] = {
	id:'toolbarAddBtnId',text:'放弃修改',
	iconCls:'icon-undo',
	handler:function(){reject();}
}*/



/**
datagrid ，后面在condition.js中init了
*/
var datagrid={
	datagridId:"list",
	datagridUrl:"owWorkPlanAjax_queryListBySearch.action",
	datagridObj:{
		/*title:"查询列表",*/
		nowrap:true,
		autoRowHeight:false,
		fit:true,
		columns:datagrid_columns,//datasoure in data.js
		frozenColumns:datagrid_frozenColumns,
		toolbar:datagrid_toolbarBtns,
		rownumbers:true,
		showFooter:true,
		singleSelect: true,
		/*onClickRow: editRow,*/
		onRowContextMenu:onRowContextMenu
	}
};

var isFirstLoadSuccess = true;
datagrid.datagridObj.onLoadSuccess=function(){
	$('#'+datagrid.datagridId).datagrid('loaded');
	$("#queryBtn_Filter").removeAttr("disabled");
	$('.editcls').linkbutton({plain:true,iconCls:'icon-edit'});  //给每行后的button加easyui样式
	$('.delcls').linkbutton({plain:true,iconCls:'icon-remove'});
	//行尾button显示或隐藏
	$("[linetail='edit']").hide();
	authRequestUrl(_formEditAction,function(authResult){
		if(authResult){
			$("[linetail='edit']").show();
		}
	});
	$("[linetail='del']").hide();
	authRequestUrl(_formRemoveAction,function(authResult){
		if(authResult){
			$("[linetail='del']").show();
		}
	});

	//根据防问权限控制toolbar button显示或隐藏
	if (isFirstLoadSuccess) {
	$('#toolbarAddBtnId').hide();
	authRequestUrl(_formAddAction,function(authResult){
	 if(authResult){
	     $('#toolbarAddBtnId').show();
	  }
	});
	isFirstLoadSuccess = false;
	}


	//行首菜单button,打开菜单列表
	$.each($("a"),function(index,value){
		var obj = $(this);
		var rowIndex1 = obj.attr("rowIndex");
		if(rowIndex1 != undefined){
			obj.mouseover(function(e){
				var obj = $(this);
				var offset = obj.offset();
				e.preventDefault();
				rowIndex = rowIndex1;
				var selected=$("#list").datagrid('getRows'); //获取所有行集合对象
				if(selected[rowIndex] == null){return;}
				id = selected[rowIndex].id; //index为当前右键行的索引，指向当前行对象
				$('#rightMenuDiv').menu('show', {
					left:offset.left+47,
					top:offset.top
				});
			});
		}
	});
};



//添加右击菜单内容
var id;
var rowIndex;
function onRowContextMenu(e, rowIndex1, rowData){
	e.preventDefault();
	rowIndex = rowIndex1;
	var selected=$("#list").datagrid('getRows'); //获取所有行集合对象
	if(selected[rowIndex] == null){return;}
	id = selected[rowIndex].id; //index为当前右键行的索引，指向当前行对象
	$('#rightMenuDiv').menu('show', {
		left:e.pageX,
		top:e.pageY
	});
};




/**
打开AddDialog时，加载默认值
*/
var _defaultValueJOStr = {}


/**
所有列备份一下，一会隐藏了，可以恢复
*/
var _columnDivArray = new Array();
_columnDivArray[_columnDivArray.length] = '#_a2user_id_line';
_columnDivArray[_columnDivArray.length] = '#_ow_area_id_line';
_columnDivArray[_columnDivArray.length] = '#_start_time_line';
_columnDivArray[_columnDivArray.length] = '#_end_time_line';
_columnDivArray[_columnDivArray.length] = '#_ow_work_type_line';
_columnDivArray[_columnDivArray.length] = '#_ow_offpost_event_type_line';
_columnDivArray[_columnDivArray.length] = '#_content_line';


